﻿namespace Hidistro.UI.Web.Pay
{
    using Hidistro.Entities.Orders;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Member;
    using Hidistro.SaleSystem.Shopping;
    using Hishop.Weixin.Pay;
    using Hishop.Weixin.Pay.Notify;
    using System;
    using System.Web.UI;

    public class wx_Pay : Page
    {
        protected OrderInfo Orders;
        protected JSShop.Model.OrderInfo Order;
        protected string OrderId;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Order = new JSShop.Model.OrderInfo();
            SiteSettings masterSettings = SettingsManager.GetMasterSettings(false);
            PayNotify payNotify = new NotifyClient(masterSettings.WeixinAppId, masterSettings.WeixinAppSecret, masterSettings.WeixinPartnerID, masterSettings.WeixinPartnerKey, masterSettings.WeixinPaySignKey).GetPayNotify(base.Request.InputStream);
            if (payNotify != null)
            {
                this.OrderId = payNotify.PayInfo.OutTradeNo;
                //this.Order = ShoppingProcessor.GetOrderInfo(this.OrderId);
                this.Order = new Juse.Interface.OrderInterface().GetOrderInfo(this.OrderId);
                if (this.Order == null)
                {
                    base.Response.Write("success");
                }
                else
                {
                    this.Order.GatewayOrderId = payNotify.PayInfo.TransactionId;
                    this.UserPayOrder();
                }
            }
        }

        private void UserPayOrder()
        {
            if (this.Order.OrderStatus == JSShop.Enum.OrderStatus.BuyerAlreadyPaid)
            {
                base.Response.Write("success");
            }
            else if (this.Orders.CheckAction(OrderActions.BUYER_PAY) && TradeHelper.UserPayOrder(this.Order, false))
            {
                this.Orders.OnPayment();
                base.Response.Write("success");
            }
        }
    }
}

